package org.todo.controller;

import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;

import javax.portlet.PortletSession;
import org.springframework.validation.BindException;
import org.springframework.web.portlet.mvc.SimpleFormController;

/**
 * Controller that handles the search action requests for todo items
 * @author Matthias Braunhofer
 */
public class SearchTodoController extends SimpleFormController {
  
    public SearchTodoController() {
        setCommandClass(SearchCommand.class);
    }

    /**
     * Action handler method that handles the submit action of the search form
     * @param request
     * @param response
     * @param command
     * @param errors
     * @throws java.lang.Exception
     */
    @Override
    protected void onSubmitAction(ActionRequest request, ActionResponse response, Object command, BindException errors) throws Exception {
        SearchCommand searchCommand = (SearchCommand)command;

        //remove / set session attributes and map rendering phase to todosController
        request.getPortletSession().removeAttribute("selectedCategory");
        request.getPortletSession().setAttribute("searchCommand", searchCommand, PortletSession.PORTLET_SCOPE);
        response.setRenderParameter("action", "todos");
    }
}
